home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / SDKs / PCI Driver Development Kit / • Tools / Utility / LogLibrary 950622 / Src / LogFormatTimestamp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-20  |  3.0 KB  |  88 lines  |  [TEXT/MPCC]

  1. /*                                LogDataFormatTimestamp.c                            */
  2. /*
  3.  * LogDataFormatTimestamp.c
  4.  * Copyright © 1992-94 Apple Computer Inc. All Rights Reserved.
  5.  * Programmed by Martin Minow,
  6.  *    Internet:    minow@apple.com
  7.  *    AppleLink:    MINOW
  8.  *
  9.  * Edit History
  10.  *    93.01.09 MM        First public release
  11.  *    93.07.09 MM        Reformatted for 80-column page. No substantive changes.
  12.  *    94.12.10 MM        Redone (from AuditEntryFormat) for the Driver Log.
  13.  */
  14. #include "LogLibrary.h"
  15. #ifndef THINK_C /* Temp until headers stabalize */
  16. #include <Types.h>
  17. #include <OSUtils.h>
  18. #endif
  19.  
  20. #define NUL    '\0'
  21. #define AppendChar(result, theChar)    do {                \
  22.         StringPtr        _dst = (result);                \
  23.         _dst[++_dst[0] & 0xFF] = theChar;                \
  24.     } while (0)
  25. static void                    AppendUnsignedLeadingZeros(
  26.         StringPtr                result,
  27.         unsigned long            value,
  28.         short                    digits,
  29.         short                    terminator
  30.     );
  31.  
  32. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  33.  * FormatLogEntryTimestamp
  34.  *
  35.  * Format a converted log entry timestamp (prepared by ConvertLogEntryTimestamp). The
  36.  * result is appended to result as a fixed-length string: "yyyy.mm.dd hh.mm.ss.msec"
  37.  * FormatLogEntryTimestamp does not use the system date formatting routines. Note
  38.  * that the fractional seconds are truncated to the nearest lower millisecond, even
  39.  * though nanosecond precision is potentially available. This function does not
  40.  * require a MixedMode switch.
  41.  */
  42. pascal void
  43. FormatLogEntryTimestamp(
  44.         StringPtr                result,                    /* Append to this string    */
  45.         const DateTimeRec        *dateTimeRec,            /* Year, Month, Day etc.    */
  46.         UInt32                    residualNanoseconds        /* Fractional second        */
  47.     )
  48. {
  49. /*
  50.  * Choose kSecondFraction    1000000L to convert nanoseconds to milliseconds
  51.  * Choose kSecondFraction       1000L to convert nanoseconds to microseconds
  52.  * Choose kSecondFraction          1L to display nanoseconds in all their glory.
  53.  */
  54. #define kSecondFraction    1000L
  55.         AppendUnsignedLeadingZeros(result, dateTimeRec->year,    4, '.');
  56.         AppendUnsignedLeadingZeros(result, dateTimeRec->month,    2, '.');
  57.         AppendUnsignedLeadingZeros(result, dateTimeRec->day,    2, ' ');
  58.         AppendUnsignedLeadingZeros(result, dateTimeRec->hour,    2, ':');
  59.         AppendUnsignedLeadingZeros(result, dateTimeRec->minute,    2, ':');
  60.         AppendUnsignedLeadingZeros(result, dateTimeRec->second,    2, '.');
  61. #if kSecondFraction == 1000L /* Microseconds */
  62.         AppendUnsignedLeadingZeros(result, residualNanoseconds / 1000L, 6, NUL);
  63. #elif kSecondFraction == 1000000L /* Milliseconds */
  64.         AppendUnsignedLeadingZeros(result, residualNanoseconds / 1000000L,    3, NUL);
  65. #else /* Nanoseconds */
  66.         AppendUnsignedLeadingZeros(result, residualNanoseconds, 9, NUL);
  67. #endif
  68. }
  69.  
  70. /*
  71.  * Output an n-digit decimal value with leading zeros.
  72.  */
  73. static void
  74. AppendUnsignedLeadingZeros(
  75.         StringPtr                result,
  76.         unsigned long            value,
  77.         short                    digits,
  78.         short                    terminator
  79.     )
  80. {
  81.         if (--digits > 0)
  82.             AppendUnsignedLeadingZeros(result, value / 10, digits, NUL);
  83.         AppendChar(result, (value % 10) + '0');
  84.         if (terminator != NUL)
  85.             AppendChar(result, terminator);
  86. }
  87.  
  88.